//gcc $(mysql_config --cflags) $(mysql_config --libs) xxx.c

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include "../chatter_global.h"

#define MMUUTTEEXX				//实践证明这是非常有效的

void *select_from_auth(void *);
void *show_tables(void *);

static int global_counter = 0;
MYSQL *conn;

#ifdef MMUUTTEEXX
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;	//default kind of 'fast'
#endif

int main()
{

	char *server = "localhost";
	char *user = "chatter";
	char *passwd = "zhimakaimen";
	char *database = "chatter_db";
	pthread_t thread_[50];

	int counter = 0;

	conn = mysql_init(NULL);

	if(!mysql_real_connect(conn, server, user, passwd, database, 0, NULL, 0))
	{
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(1);
	}

	while(counter < 1)
	{
		 pthread_create(&thread_[counter], NULL, select_from_auth, NULL);
		 counter ++;
	}
	 pthread_create(&thread_[49], NULL, show_tables, NULL);

	 while(counter >= 0)
	 {
		pthread_join(thread_[counter], NULL);
		counter --;
	 }

#ifdef MMUUTTEEXX
	pthread_mutex_destroy( &mutex);
#endif
	mysql_close(conn);	
	
	return 0;
}

void *select_from_auth(void *arg)
{

	int num_rows;
	int num_fields;
	int counter_r, counter_f;
	long affacted_r;				//受影响的行数

	MYSQL_RES *res;
	MYSQL_ROW row;


	packet_login_t	login_packet;
	
	strcpy((login_packet.from), "1234567");
	strcpy((login_packet.passwd), "7654321");

	char querystatement[256];
/*
	strncat(strcat(strncat(strcpy(querystatement, "SELECT Aaccount FROM Auth WHERE Aaccount="),
					login_packet.from, strlen(login_packet.from)), 
				" AND Apasswd="), 
			login_packet.passwd, strlen(login_packet.passwd));
*/
	if(sprintf(querystatement, "SELECT Aaccount FROM Auth WHERE Aaccount = %s AND Apasswd = %s", login_packet.from, login_packet.passwd) < 0)
		exit(1);
	printf("sqlstatement is: %s\n", querystatement);
	


	/******** select * from Auth **********/

#ifdef MMUUTTEEXX
	pthread_mutex_lock( &mutex);
#endif
//	if(mysql_query(conn, "select Aaccount,Anickname from Auth where Aaccount = 0123456"))
//	if(mysql_query(conn, "Insert Into Buddylist Values('3456789','7890123')"))
	if(mysql_query(conn, querystatement))
	{
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(1);
	}

	affacted_r = mysql_affected_rows( conn );

	res = mysql_store_result(conn);
#ifdef MMUUTTEEXX
	pthread_mutex_unlock( &mutex);
#endif

	printf("Affected rows:%ld\n", affacted_r);
	if(res)
	{//there are rows
		num_rows = mysql_num_rows(res);
		num_fields = mysql_num_fields(res);
	}
	else
	{//empty set
		num_rows = num_fields = 0;
	}
	counter_r = counter_f = 0;

	while( (row = mysql_fetch_row(res)) != NULL)
	{
		counter_f = 0;
		while(counter_f < num_fields )
		{
			printf("%s\t", row[counter_f] );
			counter_f ++;
		}

		putchar('\n');
	}
	mysql_free_result(res);

	pthread_exit(NULL);
}

void *show_tables(void *arg)
{
	MYSQL_RES *res;
	MYSQL_ROW  row;
	int num_rows, num_fields;

#ifdef MMUUTTEEXX
	pthread_mutex_lock( &mutex);
#endif

	/*show tables*/
	if(mysql_query(conn, "show tables \0 n \0"))		//still can get correct result
	{//error
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(1);
	}

	res = mysql_store_result(conn);	//restore result
#ifdef MMUUTTEEXX
	pthread_mutex_unlock( &mutex);
#endif


	if(res)
	{//there are rows
		num_rows = mysql_num_rows(res);
		num_fields = mysql_num_fields(res);
	}
	else //mysql_store_result()return nothing
	{
		if(mysql_field_count(conn) == 0)
			num_rows = mysql_affected_rows(conn);
		else	//mysql_store_result()should have returned date
			fprintf(stderr, "Error:%s\n",mysql_error(conn));
	}
	printf("MySQL tables in mysql database:<show tables>\n");
	printf("-------Result have %d rows, %d fields-----\n",num_rows, num_fields);
	int counter_f, counter_r;
//	while( (row = mysql_fetch_row(res)) != NULL)
	counter_r = 0;
	while(counter_r < num_rows)
	{
		if((row = mysql_fetch_row(res)) == NULL)
			break;
		counter_f = 0;
		while(counter_f < num_fields)
		{
			printf("%s\n", row[counter_f]);
			counter_f ++;
		}
	}
	mysql_free_result(res);

	pthread_exit(NULL);

}
